package top.drinyeit.nongmao.viewmodel;

import android.content.Intent;
import android.view.View;
import android.widget.Toast;

import com.scu.miomin.shswiperefresh.core.SHSwipeRefreshLayout;

import java.util.ArrayList;
import java.util.List;

import top.drinyeit.nongmao.activity.ChildOrderActivity;
import top.drinyeit.nongmao.activity.ChildrenOrderDetailActivity;
import top.drinyeit.nongmao.adapter.OrderAdapter;
import top.drinyeit.nongmao.common.ItemClick;
import top.drinyeit.nongmao.model.OrderChildListBean;
import top.drinyeit.nongmao.model.OrderGoodsBean;
import top.drinyeit.nongmao.retrofit.HttpResponse;
import top.drinyeit.nongmao.retrofit.RetrofitMethod;
import top.drinyeit.nongmao.retrofit.subscriber.CommonSubscriber;
import top.drinyeit.nongmao.retrofit.subscriber.SubscriberListener;

import static top.drinyeit.nongmao.app.Constant.COMMON_SUCCESS;

/**
 * Created by YJY on 2018/1/13.
 */

public class ChildOrderVM {


    private ChildOrderActivity activity;
    private OrderAdapter orderAdapter;

    public ChildOrderVM(ChildOrderActivity activity) {
        this.activity = activity;
        initRefreshLayout();
        getChildOrderList();
    }

    private void initRefreshLayout() {
        activity.shSwipeRefreshLayout.setLoadmoreEnable(false);
        activity.shSwipeRefreshLayout.setOnRefreshListener(new SHSwipeRefreshLayout.SHSOnRefreshListener() {
            @Override
            public void onRefresh() {
                getChildOrderList();
            }

            @Override
            public void onLoading() {
            }

            @Override
            public void onRefreshPulStateChange(float v, int i) {

            }

            @Override
            public void onLoadmorePullStateChange(float v, int i) {

            }
        });
    }

    private void getChildOrderList() {
        CommonSubscriber<HttpResponse<List<OrderChildListBean>>> subscriber = new CommonSubscriber<>(new SubscriberListener<HttpResponse<List<OrderChildListBean>>>() {
            @Override
            public void onError(String e, int code) {
                activity.shSwipeRefreshLayout.finishRefresh();
                Toast.makeText(activity, "获取列表失败", Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onNext(final HttpResponse<List<OrderChildListBean>> o) {
                activity.shSwipeRefreshLayout.finishRefresh();
                if (o.getCode() == COMMON_SUCCESS) {
                    List<OrderGoodsBean> data = null;
                    try {
                        data = handleList(o.getData());
                    } catch (CloneNotSupportedException e) {
                        e.printStackTrace();
                    }
                    if (data != null && data.size() > 0) {
                        orderAdapter = new OrderAdapter(activity, data, new ItemClick() {
                            @Override
                            public void onItemClick(View view, int position) {
                                switch (orderAdapter.getItemViewType(position)) {
                                    case 1: //点击订单列表头
                                        // TODO: 2018/1/13 点击订单列表头
                                        break;
                                    case 2: //点击订单列表体
                                        Intent intent = new Intent(activity, ChildrenOrderDetailActivity.class);
                                        intent.putExtra("order_id", orderAdapter.getData().get(position).getChild_order_id());
                                        activity.startActivity(intent);
                                        break;
                                    default:
                                        break;
                                }
                            }
                        });
                        activity.childOrderList.setAdapter(orderAdapter);
                    }
                }
            }
        });
        RetrofitMethod.getInstance().getChildOrderList(subscriber, activity.getIntent().getStringExtra("parent_order_id"));
    }

    private List<OrderGoodsBean> handleList(List<OrderChildListBean> list) throws CloneNotSupportedException {
        List<OrderGoodsBean> temp = new ArrayList<>();
        if (null == list || list.size() == 0) {
            return temp;
        }
        int total = 0;
        for (int i = 0; i < list.size(); i++) {
            ++total;
            OrderGoodsBean orderGoodsBean = (OrderGoodsBean) list.get(i).getCommodity_list().get(0).clone();
            orderGoodsBean.setType(1);
            orderGoodsBean.setPosition(i);
            OrderChildListBean flag = list.get(i);
            orderGoodsBean.setStore_id(flag.getStore_id());
            orderGoodsBean.setStore_name(flag.getStore_name());
            orderGoodsBean.setChild_order_id(flag.getChildren_order_info().getId());
            orderGoodsBean.setOrder_state(flag.getChildren_order_info().getOrder_state());
            orderGoodsBean.setExpress_price(flag.getChildren_order_info().getExpress_price());
            orderGoodsBean.setOrder_price(flag.getChildren_order_info().getOrder_price());
            orderGoodsBean.setCommodity_total_sum(flag.getChildren_order_info().getCommodity_total_sum());
            orderGoodsBean.setAppraise_state(flag.getChildren_order_info().getAppraise_state());
            temp.add(orderGoodsBean);
            for (int k = 0; k < flag.getCommodity_list().size(); k++) {
                ++total;
                OrderGoodsBean orderGoodsBean1 = flag.getCommodity_list().get(k);
                orderGoodsBean1.setType(2);
                orderGoodsBean1.setPosition(i);
                orderGoodsBean1.setOrder_state(flag.getChildren_order_info().getOrder_state());
                orderGoodsBean1.setChild_order_id(flag.getChildren_order_info().getId());
                temp.add(orderGoodsBean1);
            }
            ++total;
            OrderGoodsBean orderGoodsBean2 = (OrderGoodsBean) orderGoodsBean.clone();
            orderGoodsBean2.setType(3);
            temp.add(orderGoodsBean2);
        }
        return temp;
    }
}